package com.example.hello.client.Nav;

import com.example.hello.BootstrapNav;
import com.github.gwtbootstrap.client.ui.constants.Alignment;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ComponentConnector;
import com.vaadin.client.ConnectorHierarchyChangeEvent;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentContainerConnector;
import com.vaadin.shared.Connector;
import com.vaadin.shared.ui.Connect;

import java.util.Iterator;

@Connect(BootstrapNav.class)
public class BootstrapNavConnector extends AbstractComponentContainerConnector {

    @Override
    protected Widget createWidget() {
        return GWT.create(VBootstrapNav.class);
    }

    @Override
    public VBootstrapNav getWidget() {
        return (VBootstrapNav) super.getWidget();
    }

    @Override
    public BootstrapNavState getState() {
        return (BootstrapNavState) super.getState();
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        BootstrapNavState state = getState();

        VBootstrapNav nav = getWidget();

        Iterator<Connector> itr = state.getNavs().iterator();
        while (itr.hasNext()) {
            nav.add(((ComponentConnector) itr.next()).getWidget());
        }
        nav.setAlignment(Alignment.valueOf(state.getAlignment()));
    }

    @Override
    public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent connectorHierarchyChangeEvent) {

    }

    @Override
    public void updateCaption(ComponentConnector connector) {

    }

}